<?php	 
 		mysql_connect("localhost", "web964","pass17") or die ("Connection failed.");
		mysql_select_db("usr_web964_1") or die ("Database does not exist.");
				
		$erg = mysql_query("SELECT id, name, targetvalue, value, startvalue, priority, measurename, fk_measuretypeid
		 FROM goal
		  WHERE goal.fk_userid=".$_SESSION['userid'])		  
		   or die ("MySQL-Error: " . mysql_error());
		
		if (mysql_num_rows($erg) == 0){
			echo "User ".$_SESSION['username']." has no goals.";
		}else{
?>
			<table class="colored">
				<tr>
					<th>Goal<br/>Name</th>
					<th>Subgoals</th>
					<th>Deadline</th>					
					<th>Measure<br/>Name</th>
					<th>Measure<br/>Type</th>
					<th>Progress</th>
					<th>Percent</th>										
					<th>Start<br/>Value</th>				
					<th>Current<br/>Value</th>
					<th>Target<br/>Value</th>					
					<th>Priority</th>
					<th></th>
					<th></th>
				</tr>	
<?php					
					while($row = mysql_fetch_object($erg)){
						$hasSubgoals = true;		
						echo "<tr>";
						echo "<td><a href=\"goalinfo.php?id=$row->id\">$row->name</a></td>";
						$goalid = $row->id;
						$result_subgoalsnumber = mysql_query("SELECT id, count(id) AS subgoalscounter FROM subgoal WHERE fk_goalid='$goalid'")
						or die ("MySQL-Error: " . mysql_error());
						while($row2 = mysql_fetch_object($result_subgoalsnumber)){
							if($row2->subgoalscounter == 0){
								$hasSubgoals = false;
						 		echo "<td>0 subgoals</td>";
						 	}else if($row2->subgoalscounter == 1){
						 		$hasSubgoals = true;
						 		echo "<td>1 subgoal</td>";
						 	}else{
						 		$hasSubgoals = true;
						 		echo "<td>$row2->subgoalscounter subgoals</td>";
						 	}											
						}
						$deadlineSet = false;						
						$result_deadline = mysql_query("SELECT date FROM goaldeadline WHERE fk_goalid='$goalid'")
						or die ("MySQL-Error: " . mysql_error());
						while($row3 = mysql_fetch_object($result_deadline)){
							if (mysql_num_rows($result_deadline) == 1){
								echo "<td>$row3->date</td>";
								$deadlineSet = true;
							}else if (mysql_num_rows($result_deadline) == 0){
								echo "<td>-</td>";
								$deadlineSet = true;
							}
						}
						if (!$deadlineSet){
							echo "<td>-</td>";
						}
						
						echo "<td>$row->measurename</td>";
						
						$goalmeasuretype = "";
						$result_measuretype = mysql_query("SELECT typename FROM measuretype WHERE id='$row->fk_measuretypeid'")
						or die ("MySQL-Error: " . mysql_error());						
						while($row4 = mysql_fetch_object($result_measuretype)){									
							echo "<td>$row4->typename</td>";
							$goalmeasuretype = $row4->typename;														
						}
						
							
						if ($hasSubgoals == true){
							$result_subgoals = mysql_query(
							"SELECT subgoal.id AS subgoalid, startvalue, targetvalue, value, measuretype.typename 
							FROM subgoal, measuretype
							 WHERE subgoal.fk_goalid='$goalid' AND measuretype.id=subgoal.fk_measuretypeid")
							or die ("MySQL-Error: " . mysql_error());	
							
							$subgoalsPercentageArray = array();					
							while($row5 = mysql_fetch_object($result_subgoals)){									
								$startV = $row5->startvalue;
								$targetV = $row5->targetvalue;
								$currentV = $row5->value;								
								$subgoalmeasureType = $row5->typename;
								
								#echo "computed : ".computePercentage(floatval($startV), floatval($currentV), floatval($targetV), $subgoalmeasureType);
								if ($subgoalmeasureType == "yes/no"){
									$subgoalsPercentageArray[] = computePercentage($startV, $currentV, $targetV, $subgoalmeasureType);
								}else{
									$subgoalsPercentageArray[] = computePercentage(floatval($startV), floatval($currentV), floatval($targetV), $subgoalmeasureType);
								}
							}	
							
							$totalPercentageOfGoal = computeTotalPercentageOfGoal($subgoalsPercentageArray);
							$canvasid = "canvas_".$goalid."_0"."_100"."_$totalPercentageOfGoal";
							
							#test
							#foreach ($subgoalsPercentageArray as $i  => $value) {
								#echo "array: $subgoalsPercentageArray[$i]<br/>";			
							#}
							#end of test
								
							echo "<td><canvas id=\"$canvasid\" width=\"100\" height=\"10\"></canvas></td>";
							echo "<td></td>";					
							
							echo "<td>0</td>";							
							echo "<td>".round($totalPercentageOfGoal, 2)."</td>";
							echo "<td>100</td>";
													
						}else{	
							$canvasid = "canvas_".$goalid."_$row->startvalue"."_$row->targetvalue"."_$row->value";
							echo "<td><canvas id=\"$canvasid\" width=\"100\" height=\"10\"></canvas></td>";
							echo "<td></td>";					
							echo "<td>$row->startvalue</td>";
							echo "<td>$row->value</td>";					
							echo "<td>$row->targetvalue</td>";
						}	
						
						$subgoalpriority = "";
						switch ($row->priority) {
    						case "ui":
        						$subgoalpriority = "urgent / important";
        						break;
    						case "uni":
        						$subgoalpriority = "urgent / unimportant";
        						break;
	   						case "nui":
	        						$subgoalpriority = "unurgent / important";
	       						break;
	       					case "nuni":
	        						$subgoalpriority = "unurgent / unimportant";
	       					break;
						}
						echo "<td>$subgoalpriority</td>";
						echo "<td><a href=\"index.php?page=edit&goalid=$goalid\" ><img width=\"35.5px\" src=\"lib/img/buttonEdit.png\"/></a></td>";
						echo "<td><a href=\"index.php?page=overview&del=$goalid\"><img width=\"50px\" src=\"lib/img/buttonDelete.png\"/></a></td>";
						echo "</tr>";
					}
				
			echo "</table>";
		}
		
	function computePercentage($startvalue, $currentvalue, $targetvalue, $measuretype){
		
		if ($measuretype == "integer" OR $measuretype == "float" OR $measuretype == "money" OR $measuretype == "percent"){
			if (abs($targetvalue - $startvalue) == 0){
				return 0;
			}
			/*echo "start: $startvalue<br/>";
			echo "current: $currentvalue<br/>";
			echo "target: $targetvalue<br/>";
			echo "type: $measuretype<br/>";
			echo "return: abs($targetvalue - $startvalue) * 100 / abs($currentvalue - $startvalue)<br/>";*/
			return abs(($currentvalue - $startvalue) * 100) / (abs($targetvalue - $startvalue));
		}else if ($startvalue == "no" || $targetvalue == "yes"){
			if ($currentvalue == "yes"){
				return 100;
			}else{
				return 0;
			}
		}
	}
	
	function computeTotalPercentageOfGoal($subgoalsProgressArray){
		if (count($subgoalsProgressArray) == 0){
			return 0;
		}		
		
		$sum = 0;
		foreach ($subgoalsProgressArray as $i  => $value) {
			$sum += $subgoalsProgressArray[$i];			
		}		
		return $sum / count($subgoalsProgressArray);	
	}
?>